app.factory(ꞌStackServiceꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 83
rs 8.7468
c 1
b 0
f 0
cc 1
nc 1
nop 3

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 12 1
A ��) 0 9 3
B ��) 0 22 5
A ��) 0 9 3
A ��) 0 3 1
A ��) 0 6 2
A ��) 0 12 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
3
 *
4
 * @author Julius Härtl <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *  
8
 *  This program is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU Affero General Public License as
10
 *  published by the Free Software Foundation, either version 3 of the
11
 *  License, or (at your option) any later version.
12
 *  
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU Affero General Public License for more details.
17
 *  
18
 *  You should have received a copy of the GNU Affero General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *  
21
 */
22
23
app.factory('StackService', function(ApiService, $http, $q){
24
    var StackService = function($http, ep, $q) {
25
        ApiService.call(this, $http, ep, $q);
26
    };
27
    StackService.prototype = angular.copy(ApiService.prototype);
28
    StackService.prototype.fetchAll = function(boardId) {
29
        var deferred = $q.defer();
30
        var self=this;
31
        $http.get(this.baseUrl +'/'+boardId).then(function (response) {
32
            self.clear();
33
            self.addAll(response.data);
34
            deferred.resolve(self.data);
35
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
36
            deferred.reject('Error while loading stacks');
37
        });
38
        return deferred.promise;
39
    };
40
41
    StackService.prototype.fetchArchived = function(boardId) {
42
        var deferred = $q.defer();
43
        var self=this;
44
        $http.get(this.baseUrl +'/'+boardId+'/archived').then(function (response) {
45
            self.clear();
46
            self.addAll(response.data);
47
            deferred.resolve(self.data);
48
        }, function (error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
            deferred.reject('Error while loading stacks');
50
        });
51
        return deferred.promise;
52
    };
53
54
    StackService.prototype.addCard = function(entity) {
55
        if(!this.data[entity.stackId].cards) {
56
            this.data[entity.stackId].cards = [];
57
        }
58
        this.data[entity.stackId].cards.push(entity);
59
    };
60
61
    StackService.prototype.reorder = function(entity, order) {
62
        // assign new order
63
        for(var i=0, j=0;i<this.data[entity.stackId].cards.length;i++) {
64
            if(this.data[entity.stackId].cards[i].id === entity.id) {
65
                this.data[entity.stackId].cards[i].order = order;
66
            }
67
            if(j === order) {
68
                j++;
69
            }
70
            if(this.data[entity.stackId].cards[i].id !== entity.id) {
71
                this.data[entity.stackId].cards[i].order = j++;
72
            }
73
        }
74
        // sort array by order
75
        this.data[entity.stackId].cards.sort(function(a,b) {
76
            if (a.order < b.order)
77
                return -1;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
78
            if (a.order > b.order)
79
                return 1;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
80
            return 0;
81
        });
82
    };
83
84
    StackService.prototype.updateCard = function(entity) {
85
        var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
86
        var cards = this.data[entity.stackId].cards;
87
        for(var i=0;i<cards.length;i++) {
88
            if(cards[i].id == entity.id) {
89
                cards[i] = entity;
90
            }
91
        }
92
    };
93
    StackService.prototype.removeCard = function(entity) {
94
        var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
95
        var cards = this.data[entity.stackId].cards;
96
        for(var i=0;i<cards.length;i++) {
97
            if(cards[i].id == entity.id) {
98
                cards.splice(i, 1);
99
            }
100
        }
101
    };
102
    
103
    service = new StackService($http, 'stacks', $q);
0 ignored issues
show
Bug introduced by
The variable service seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.service.
Loading history...
104
    return service;
105
});
106
107